package io.renren.modules.app.controller;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("app/Sms")
public class SmsController {
    @PostMapping("/sendMessage")
    public String baiduSms(@RequestParam String iPhone) {
        int i = (int) ((Math.random() * 9 + 1) * Math.pow(10, 5));//6位验证码
 /*       //先从redis获取该手机号短信是否过期
        Integer code = redisCache.getCacheObject(Constants.PYXD_SMS + phone);
        if (code != null) {
            return phone + ":" + code + "验证码已发送！";
        }
        //自己百度的密钥
        String ACCESS_KEY_ID = clientIdKey;
        String SECRET_ACCESS_KEY = clientSecretKey;
        String ENDPOINT = "http://smsv3.bj.baidubce.com";

        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
        config.setEndpoint(ENDPOINT);
        SmsClient client = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(iPhone);
        //这里模板填写自己的
        request.setSignatureId("sms-sign-******");
        request.setTemplate("sms-tmpl-AP*****");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("code", String.valueOf(i));
        contentVar.put("time", "5");
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        if (response != null && response.isSuccess()) {
            //发送成功存入redis
          //  redisCache.setCacheObject(Constants.PYXD_SMS + phone, i, 5, TimeUnit.MINUTES);
            return "发送成功";
        } else {
            return "发送失败";
        }*/
        return "abs";
    }

}
